package com.apes.hr.recruit.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.Employee;
import com.apes.hr.base.model.Staffing;
import com.apes.hr.base.model.WorkPost;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：招聘申请单
 * @author HuJianlin
 * @create 2018-05-05 15:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = RecruitOrder.CONST_TABLE_NAME)
public class RecruitOrder extends Model {
    public static final String CONST_TABLE_NAME = "hr_Recruit_Order";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "招聘申请单")
    private String id;   //申请单号

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //所属公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id", referencedColumnName = "id")
    private Branch branch; //招聘部门-取行政部门

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "workPost_id", referencedColumnName = "id")
    private WorkPost workPost; //招聘岗位

    @Column(name = "recruit_qty", nullable = false, columnDefinition = "number(8,0) default 0")
    private int currentRecruitQty = 0;  //本次申请招聘人数

    @Column(name = "apply_qty", nullable = false, columnDefinition = "number(8,0) default 0")
    private int applyQty = 0;  //应聘人数

    @Column(name = "entry_qty", nullable = false, columnDefinition = "number(8,0) default 0")
    private int entryQty = 0;  //入职人数

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "staffing_id", referencedColumnName = "id")
    private Staffing staffing;  //人员编制

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "entry_date", nullable = false)
    private Date entryDate;  //要求入职日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "recruit_nature", referencedColumnName = "id")
    private Enumeration recruitNature; // 招聘性质(全日制、兼职)

    @Column(name = "note", length = 250)
    private String note; //备注

    @Column(name = "job_duty", length = 4000)
    private String jobDuty; // 岗位职责

    @Column(name = "job_require", length = 4000)
    private String jobRequire; // 任职要求

    @Column(name = "state", length = 10, nullable = false)
    private String state; //状态：   ('new', '创建'), ('approve', '审核'), ('done', '已完成'), ('cancel', '已取消')

    @Column(name = "approve_date")
    private Date approveDate; //审批日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    private PartyRole approveUid; //审批人


    @Column(name = "stop_date")
    private Date stopDate; //停止日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "stop_uid", referencedColumnName = "id")
    private PartyRole stopUid; //停止人

    @JSONField(serialize = false)
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "recruitOrder")
    private Set<Employee> employeeSets = new HashSet<>();//缺岗导致人

}
